package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.TBoxInfo;
import com.ruoyi.system.mapper.TBoxInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TDeviceInfoMapper;
import com.ruoyi.system.domain.TDeviceInfo;
import com.ruoyi.system.service.ITDeviceInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-20
 */
@Service
public class TDeviceInfoServiceImpl implements ITDeviceInfoService 
{
    @Autowired
    private TDeviceInfoMapper tDeviceInfoMapper;
    @Autowired
    private TBoxInfoMapper tBoxInfoMapper;

    /**
     * 查询设备数据
     * 
     * @param deviceCode 设备数据主键
     * @return 设备数据
     */
    @Override
    public TDeviceInfo selectTDeviceInfoByDeviceCode(String deviceCode)
    {
        return tDeviceInfoMapper.selectTDeviceInfoByDeviceCode(deviceCode);
    }

    /**
     * 查询设备数据列表
     * 
     * @param tDeviceInfo 设备数据
     * @return 设备数据
     */
    @Override
    public List<TDeviceInfo> selectTDeviceInfoList(TDeviceInfo tDeviceInfo)
    {
        return tDeviceInfoMapper.selectTDeviceInfoList(tDeviceInfo);
    }

    /**
     * 新增设备数据
     * 
     * @param tDeviceInfo 设备数据
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTDeviceInfo(TDeviceInfo tDeviceInfo)
    {
        if (StringUtils.isBlank(tDeviceInfo.getDeviceCode())) {
            throw new RuntimeException("扫码异常，未取到设备编号");
        }
        TDeviceInfo findDeviceInfo = selectTDeviceInfoByDeviceCode(tDeviceInfo.getDeviceCode());
        if (findDeviceInfo!=null){
            throw new RuntimeException("此设备已被装箱");
        }
        int allDevice = tBoxInfoMapper.getDeviceCount(tDeviceInfo.getBoxCode());

        TBoxInfo tBoxInfo = tBoxInfoMapper.selectTBoxInfoByBoxCode(tDeviceInfo.getBoxCode());
        if (tBoxInfo==null) {
            throw new RuntimeException("此箱数据异常，请换箱");
        }
        if (tBoxInfo!=null){
            if(allDevice>=tBoxInfo.getBoxCapacity()){
                throw new RuntimeException("此箱已满，请换箱");
            }
            if(allDevice<tBoxInfo.getBoxCapacity()){
                tBoxInfo.setBoxState("0");
            }else if(allDevice>=tBoxInfo.getBoxCapacity()){
                tBoxInfo.setBoxState("1");
            }
            tBoxInfo.setUpdateTime(DateUtils.getNowDate());
            tBoxInfoMapper.updateTBoxInfo(tBoxInfo);
        }
        tDeviceInfo.setCkState("0");
        tDeviceInfo.setCreateId(SecurityUtils.getUserId());
        tDeviceInfo.setCreateName(SecurityUtils.getLoginUser().getUser().getNickName());
        tDeviceInfo.setCreateTime(DateUtils.getNowDate());
        return tDeviceInfoMapper.insertTDeviceInfo(tDeviceInfo);
    }

    /**
     * 修改设备数据
     * 
     * @param tDeviceInfo 设备数据
     * @return 结果
     */
    @Override
    public int updateTDeviceInfo(TDeviceInfo tDeviceInfo)
    {
        //单个设备出库
        TDeviceInfo old = selectTDeviceInfoByDeviceCode(tDeviceInfo.getDeviceCode());
        if(!old.getCkState().equals(tDeviceInfo.getCkState()) && tDeviceInfo.getCkState().equals("1")){
            tDeviceInfo.setCkTime(DateUtils.getNowDate());
        }
        return tDeviceInfoMapper.updateTDeviceInfo(tDeviceInfo);
    }

    /**
     * 根据箱子编号 修改设备数据
     *
     * @param tDeviceInfo 设备数据
     * @return 结果
     */
    @Override
    public int updateTDeviceByBoxCode(TDeviceInfo tDeviceInfo)
    {
        return tDeviceInfoMapper.updateTDeviceByBoxCode(tDeviceInfo);
    }

    /**
     * 批量删除设备数据
     * 
     * @param deviceCodes 需要删除的设备数据主键
     * @return 结果
     */
    @Override
    public int deleteTDeviceInfoByDeviceCodes(String[] deviceCodes)
    {
        return tDeviceInfoMapper.deleteTDeviceInfoByDeviceCodes(deviceCodes);
    }

    /**
     * 删除设备数据信息
     * 
     * @param deviceCode 设备数据主键
     * @return 结果
     */
    @Override
    public int deleteTDeviceInfoByDeviceCode(String deviceCode)
    {
        return tDeviceInfoMapper.deleteTDeviceInfoByDeviceCode(deviceCode);
    }

    /**
     * 设备批量出库
     *
     * @param deviceCodes
     * @return 结果
     */
    public int  ckDeviceInfoByDeviceCodes(TDeviceInfo tDeviceInfo, String[] deviceCodes){
        return tDeviceInfoMapper.ckDeviceInfoByDeviceCodes(tDeviceInfo.getCustomerAddress(),tDeviceInfo.getCustomerName(),tDeviceInfo.getCustomerPhone(),tDeviceInfo.getCustomerInfo(),tDeviceInfo.getCustomerRemark(),deviceCodes);
    }
}
